package lumis.portal.presentation.perspective.admin;

import lumis.portal.presentation.tag.page.RenderHolderComponent;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderResponse;

/**
 * Behavior that is used as base for behaviors that process a drop of 
 * an interface.
 *
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("serial")
abstract class DropInterfaceBehavior extends AbstractDefaultAjaxBehavior
{
	/**
	 * Inserts an interface instance in a holder, according to the current request.
	 * @param serviceInterfaceInstanceId the service interface instance identifier.
	 * @param target the AJAX request target.
	 * @since 6.0.0
	 */
	protected void insertInterfaceInstance(final String serviceInterfaceInstanceId, final AjaxRequestTarget target)
	{
		// if insert near existing interface instance
		MarkupContainer container = (MarkupContainer) getComponent();
		final String destII = container.getRequest().getParameter("destII");
		if (destII != null && !"null".equals(destII))
		{
			final boolean before = "0".equals(container.getRequest().getParameter("pos"));
			
			// add srcII near destII according to before
			container.visitChildren(RenderHolderComponent.class, new IVisitor<RenderHolderComponent>()
			{
				public Object component(RenderHolderComponent component)
				{
					if (component.insertServiceInterfaceInstance(serviceInterfaceInstanceId, destII, before))
					{
						target.addComponent(component);
						return STOP_TRAVERSAL;
					}
					else
					{
						return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
					}
				}
			});
		}
		else
		{
			final String destHolderId = container.getRequest().getParameter("destHolderId");
			
			// add srcII in the referenced holder
			container.visitChildren(RenderHolderComponent.class, new IVisitor<RenderHolderComponent>()
			{
				public Object component(RenderHolderComponent component)
				{
					if (destHolderId.equals(component.getHolderId()))
					{
						component.addServiceInterfaceInstance(serviceInterfaceInstanceId);
						target.addComponent(component);
						return STOP_TRAVERSAL;
					}
					else
					{
						return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
					}
				}
			});
		}
	}
	
	/**
	 * Returns the javascript function name that calls this behavior.
	 * @return the javascript function name.
	 * @since 6.0.0
	 */
	protected abstract String getFunctionName();
	
	
	@Override
	public void renderHead(IHeaderResponse response)
	{
		response.renderJavascript("function " + getFunctionName() + "(srcII, destII, destHolderId, pos)" +
				"{" +
					"wicketAjaxPost('lumis/portal/presentation/layoutfile/" + getCallbackUrl() + 
					"','srcII='+srcII+'&destII='+destII+'&pos='+pos+'&destHolderId='+LumisPortal.lumisEncodeURIComponent(destHolderId),function(){g_LumisPage.onHolderRefresh()}.bind(this),function(){}.bind(this),function(){return true;}.bind(this));" +
				"}", null);
	}
}